local function max(n1, n2)
    if (n1 == nil) then
        return n2
    end
    if (n1 >= n2) then
        return n1
    else
        return n2
    end
end

local function min(n1, n2)
    if (n1 == nil) then
        return n2
    end
    if (n1 >= n2) then
        return n2
    else
        return n1
    end
end

local function add(n1, n2)
    return n1 + n2
end

local function mul(n1, n2)
    return n1 * n2
end

local function div(n1, n2)
    return n1 / n2
end

local default = {max=nil, min=nil, add=0, mul=1, div=1}
local func = {max=max, min=min, add=add, mul=mul, div=div}

local current = redis.call("hget", KEYS[1], KEYS[2])
if current == false then
    current = default[KEYS[4]]
else
    current = tonumber(current)
end
local next = tonumber(KEYS[3])
redis.call("hset", KEYS[1], KEYS[2], func[KEYS[4]](current, next))
